package com.kl.modular.common.response;

import com.alibaba.fastjson.JSON;

/**
 * 统一JSON返回格式
 *
 * 所有ajax请求json格式需统一使用该格式，以便全局维护与全局异常处理
 *
 * @author xr_zjb
 *
 */
public class ResponseBase {

	public final static String RESPONSE_STATUS_SUCCESS = "SUCCESS";// 成功
	public final static String RESPONSE_STATUS_FAILED = "FAILED";// 失败
	public final static String RESPONSE_STATUS_ERROR = "ERROR";// 系统错误/运行时异常
	public final static String RESPONSE_MSG_ERROR = "系统繁忙";
	public final static String RESPOSNE_NOT_VALID_ERROR = "请求数据有误";
	public final static String RESPOSNE_FAILED_ERROR = "请求错误";

	/**
	 * 返回状态
	 */
	private String status = ResponseBase.RESPONSE_STATUS_FAILED;

	/**
	 * 返回数据
	 */
	private Object data;
	
	/**
	 * 返回信息
	 */
	private String msg;

	/**
	 * 错误响应
	 * @param msg 错误信息
	 * @return
	 */
	public static String errorStr(String msg){
		ResponseBase rsp = new ResponseBase();
		rsp.setMsg(msg);
		return JSON.toJSONString(rsp);
	}

	/**
	 * 错误响应
	 * @param msg 错误信息
	 * @return
	 */
	public static ResponseBase errorResponse(String msg){
		ResponseBase rsp = new ResponseBase();
		rsp.setMsg(msg);
		return rsp;
	}

	/**
	 * 响应失败
	 * @return
	 */
	public static String failed() {
		ResponseBase rsp = new ResponseBase();
		rsp.setStatus(ResponseBase.RESPONSE_STATUS_FAILED);
		rsp.setMsg(ResponseBase.RESPOSNE_FAILED_ERROR);
		return JSON.toJSONString(rsp);
	}

	/**
	 * 响应成功
	 * @return
	 */
	public static String success(){
		ResponseBase rsp = new ResponseBase();
		rsp.setStatus(ResponseBase.RESPONSE_STATUS_SUCCESS);
		return JSON.toJSONString(rsp);
	}

	/**
	 * 响应成功数据
	 * @param data
	 * @return
	 */
	public static String success(Object data){
		ResponseBase rsp = new ResponseBase();
		rsp.setStatus(ResponseBase.RESPONSE_STATUS_SUCCESS);
		rsp.setData(data);
		return JSON.toJSONString(rsp);
	}

	/**
	 * 响应成功数据
	 * @param data
	 * @return
	 */
	public static ResponseBase successRsp(Object data){
		ResponseBase rsp = new ResponseBase();
		rsp.setStatus(ResponseBase.RESPONSE_STATUS_SUCCESS);
		rsp.setData(data);
		return rsp;
	}

	/**
	 * 响应成功数据
	 * @return
	 */
	public static String successMsg(String msg){
		ResponseBase rsp = new ResponseBase();
		rsp.setStatus(ResponseBase.RESPONSE_STATUS_SUCCESS);
		rsp.setMsg(msg);
		return JSON.toJSONString(rsp);
	}



	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}

